//
//  ImageViewListBox.m
//  安好
//
//  Created by rlp on 14-12-15.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "ImageViewListBox.h"

@implementation ImageViewListBox

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame imageViews:(NSArray *)imageViews{
    self = [super initWithFrame:frame];
    if (self) {
        if (imageViews==nil||imageViews.count==0||[imageViews isEqual:[NSNull null]]) {
            self.frame = CGRectZero;
            return self;
        }
        

        
        NSInteger number = (frame.size.width-15*2)/(55+23)+1;
        if (frame.size.width==375) {
            number=4;
        }
        NSMutableArray *arraySum = [[NSMutableArray alloc]init];

        for (int i=0; i<imageViews.count/number+1; i++) {//排数
            NSMutableArray *array = [[NSMutableArray alloc]init];

            for (int j=0; j<number; j++) {//数组个数
                MyNSLog("%d",i*number+j);
                
                NSInteger counts = MIN(number*2, imageViews.count);
                
                    if (i*number+j<counts) {
                        [array addObject:[imageViews objectAtIndex:i*number+j]];
                        
                    }else{
                        break;
                    }
                

            }
            [arraySum addObject:array];

        }

        for (int i=0; i<imageViews.count/number+1; i++) {//排数
            for (int j=0; j<[[arraySum objectAtIndex:i] count]; j++) {//数组个数
                
                
            
                if (i*number+j<number*2) {
                    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-((55+23)*number-23))/2+(55+23)*j, (frame.size.height-(55*2+23))/2+(55+23)*i, 55, 55)];
                    imageview.tag = i*number+j;
                    //                [imageview setImage:[imageViews objectAtIndex:i]];
                    [imageview setImage:[[arraySum objectAtIndex:i] objectAtIndex:j]];
                    
                    
                    imageview.layer.masksToBounds = YES;
                    imageview.layer.cornerRadius = 3.0;
                    imageview.userInteractionEnabled = YES;
                    [self  addSubview:imageview];
                    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
                    [imageview addGestureRecognizer:singleTap];
                    
                    
                    
                    if (i*number+j!=MIN(imageViews.count-1, 8)) {
                        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(imageview.frame.origin.x+imageview.frame.size.width-6, imageview.frame.origin.y-6, 12, 12)];
                        [button setImage:[UIImage imageNamed:@"日志_删除"] forState:UIControlStateNormal];
                        button.tag =i*number+j;
                        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                        [self addSubview:button];
                    }

                    
                    
                    
                    
                }else{
                    break;
                }
                
                
                
                
            }
            


            
            
        }
//        self.frame =CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, frame);
        
    }
    return self;

}

-(void)setImages:(NSMutableArray *)images{
    
    _images = images;
    
    if (_images==nil||_images.count==0||[_images isEqual:[NSNull null]]) {
        self.frame = CGRectZero;
        return ;
    }
    
    [UIView animateWithDuration:0.3f animations:^{
        
        
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        
        
        
        NSInteger number = (self.frame.size.width-15*2)/(55+23)+1;
        if (self.frame.size.width==375) {
            number=4;
        }
        NSMutableArray *arraySum = [[NSMutableArray alloc]init];
        
        for (int i=0; i<_images.count/number+1; i++) {//排数
            NSMutableArray *array = [[NSMutableArray alloc]init];
            
            for (int j=0; j<number; j++) {//数组个数
                
                NSInteger counts = MIN(number*2, _images.count);
                
                if (i*number+j<counts) {
   
                        [array addObject:[_images objectAtIndex:i*number+j]];
                        
                    }else{
                        break;
                    }
            }
            [arraySum addObject:array];
            
        }
        
        for (int i=0; i<_images.count/number+1; i++) {//排数
            for (int j=0; j<[[arraySum objectAtIndex:i] count]; j++) {//数组个数
                
                
                
                if (i*number+j<number*2) {
                    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width-((55+23)*number-23))/2+(55+23)*j, (self.frame.size.height-(55*2+23))/2+(55+23)*i, 55, 55)];
                    imageview.tag = i*number+j;
                    
                    
                    [imageview setImage:[[arraySum objectAtIndex:i] objectAtIndex:j]];
                    
                    
                    imageview.layer.masksToBounds = YES;
                    imageview.layer.cornerRadius = 3.0;
                    imageview.userInteractionEnabled = YES;
                    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
                    [imageview addGestureRecognizer:singleTap];
                    
                    if (i*number+j==8) {
                        
                    }else{
                        [self  addSubview:imageview];

                    }

                    if (i*number+j!=MIN(_images.count-1, 8)) {
                        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(imageview.frame.origin.x+imageview.frame.size.width-6, imageview.frame.origin.y-6, 12, 12)];
                        [button setImage:[UIImage imageNamed:@"日志_删除"] forState:UIControlStateNormal];
                        button.tag =i*number+j;
                        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                        [self addSubview:button];
                    }
                    
                }else{
                    break;
                }
                
                
                
                
            }
            
            
            
            
            
        }

    }];
    
    
}
-(IBAction)imageClicked:(id)sender{
    UITapGestureRecognizer *top = sender;
    NSInteger tags = top.view.tag;
    [self.delegate imageViewListIndex:top.view.superview.tag subImageIndex:tags];
    
}
-(IBAction)buttonClick:(id)sender{
    UIButton *button = (UIButton*)sender;
    [self.delegate imageViewListDeleteIndex:button.superview.tag subImageIndex:button.tag];
}

@end
